package com.xhb.ui.download;

import com.teamdev.jxbrowser.download.Download;
import com.xhb.biz.constant.DownloadConstant;
import com.xhb.biz.po.DownloadPO;
import com.xhb.biz.service.DownloadFileService;
import lombok.Data;
import org.noear.solon.Solon;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 */
@Data
public class DownloadManager {

    private Map<String, Download> idDownloadMap = new ConcurrentHashMap<>();

    private static DownloadManager instance = new DownloadManager();

    public static DownloadManager getInstance() {
        return instance;
    }

    /**
     * 添加下载对象
     *
     * @param id
     * @param download
     */
    public void addDownload(String id, Download download) {
        idDownloadMap.put(id, download);
    }

    /**
     * 删除下载对象
     *
     * @param id
     */
    public void removeDownload(String id) {
        if (idDownloadMap.containsKey(id)) {
            idDownloadMap.remove(id);
        }
    }


    /**
     * 执行暂停
     *
     * @param id
     */
    public void paused(String id) {
        if (idDownloadMap.containsKey(id)) {
            Download download = idDownloadMap.get(id);
            download.pause();
            //更新表中数据
            Solon.context().getBeanAsync(DownloadFileService.class, bean -> {
                DownloadPO updatePO = DownloadPO.builder()
                        .id(id)
                        .status(DownloadConstant.STATUS_INTERRUPTED).build();
                bean.updateAsync(updatePO);
            });
        }
    }

    /**
     * 恢复
     *
     * @param id
     */
    public void resume(String id) {
        if (idDownloadMap.containsKey(id)) {
            Download download = idDownloadMap.get(id);
            if (download.isPaused()) {
                download.resume();
                //更新表中数据
                Solon.context().getBeanAsync(DownloadFileService.class, bean -> {
                    DownloadPO updatePO = DownloadPO.builder()
                            .id(id)
                            .status(DownloadConstant.STATUS_ING).build();
                    bean.updateAsync(updatePO);
                });

                //继续更新状态
                DownloadFilePanel downloadFilePanel = BottomDownloadBar.getInstance().getIdDownloadMap().get(id);
                downloadFilePanel.getDownloadPO().setStatus(DownloadConstant.STATUS_ING);
                new Thread(downloadFilePanel).start();
                BottomDownloadBar.getInstance().open();
            }
        }
    }

    /**
     * 取消
     *
     * @param id
     */
    public void cancel(String id) {
        if (idDownloadMap.containsKey(id)) {
            Download download = idDownloadMap.get(id);
            download.cancel();
            //更新掉表中数据
            Solon.context().getBeanAsync(DownloadFileService.class, bean -> {
                DownloadPO updatePO = DownloadPO.builder()
                        .id(id)
                        .status(DownloadConstant.STATUS_CANCELLED).build();
                bean.updateAsync(updatePO);

                //继续更新状态
                DownloadFilePanel downloadFilePanel = BottomDownloadBar.getInstance().getIdDownloadMap().get(id);
                downloadFilePanel.getDownloadPO().setStatus(DownloadConstant.STATUS_CANCELLED);
            });
        }
    }

}
